package nf.action;

import nf.dao.CellPhoneDao;
import nf.dao.CellPhoneDaoImpl;
import nf.entity.CellPhone;

import javax.print.attribute.PrintServiceAttributeSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/ServletPhone")
public class ServletPhone extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("usernaem");
        String password = (String)session.getAttribute("password");

        if(username!=null||password!=null){
            String name = request.getParameter("selectname");
            CellPhoneDao dao = new CellPhoneDaoImpl();
            List<CellPhone> listphone = dao.querByName(name);

            request.setAttribute("phone",listphone);

            request.getRequestDispatcher("home.jsp").forward(request,response);
        }else {
            response.sendRedirect("index.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String username =(String)session.getAttribute("username");
        String password =(String)session.getAttribute("password");

        if(username!=null||password!=null){
            request.setCharacterEncoding("utf-8");
            CellPhoneDao dao = new CellPhoneDaoImpl();
            List<CellPhone> phone = dao.quer();

            request.setAttribute("phone",phone);
            request.getRequestDispatcher("home.jsp").forward(request,response);
        }else{
            response.sendRedirect("index.jsp");
        }


    }
}
